import {isSuccessRes, PageResData, ResponseData} from "@/types/common";

export const exportExData = async (columnsDesc, reqCallback: (p: any) => Promise<ResponseData<PageResData<any>>>, params, {
  filename, ejectColsTitles = []
} = {} as any) => {
  ejectColsTitles.push('操作')
  const excel = await import('@/vendor/Export2Excel')
  columnsDesc = columnsDesc.filter(cd => !ejectColsTitles.includes(cd.title))

  const header = columnsDesc.map(cd => cd.title);
  const dataIdxList = columnsDesc.map(cd => cd.dataIndex);
  const res = await reqCallback(params);

  console.log(ejectColsTitles)
  if (isSuccessRes(res)) {
    const data = res.Result.Items.map(r => Object.keys(r).reduce((acc, k) => dataIdxList.includes(k) ? acc.concat(r[k]) : acc, []))
    console.log(data)
    console.log(header)
    excel.export_json_to_excel({
      header,
      data,
      filename,
    })
  } else {
    //  msg export error
  }
}
